<template>
  <div>
    <header>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商品管理</el-breadcrumb-item>
        <el-breadcrumb-item>商品列表</el-breadcrumb-item>
      </el-breadcrumb>
    </header>
    <main>
      <div class="title">
        <el-input
          placeholder="请输入内容"
          v-model="input3"
          class="input-with-select"
          @keyup.enter.native="search"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="search"
          ></el-button>
        </el-input>
        <el-button type="primary" @click="addGoods">添加商品</el-button>
      </div>
      <el-table :data="goodList" stripe style="width: 100%" border>
        <el-table-column type="index"> </el-table-column>
        <el-table-column prop="goods_name" label="商品名称"> </el-table-column>
        <el-table-column prop="goods_price" label="商品价格(元)">
        </el-table-column>
        <el-table-column prop="goods_weight" label="商品重量">
        </el-table-column>
        <el-table-column prop="address" label="创建时间">
          <template slot-scope="scope">
            {{ new Date(parseInt(scope.row.add_time)).toLocaleString() }}
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              @click="$message.error('后端没给接口,编辑个锤子')"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              @click="deleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </main>
  </div>
</template>
<script>
import { getGoodsList, deleteGoods } from "@/api/goods";
export default {
  name: "ShopGoods",
  data() {
    return {
      input3: "",
      currentPage: 1,
      goodList: [],
      goodPage: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      total: 0,
    };
  },
  created() {
    this.getGoodsList().then((res) => {
      if (res.data.meta.status !== 200) {
        return this.$message.error("获取商品列表失败");
      } else {
        this.$message.success("获取商品列表成功");
      }
    });
  },
  computed: {},
  methods: {
    async getGoodsList() {
      const res = await getGoodsList(this.goodPage);
      this.goodList = res.data.data.goods;
      this.total = res.data.data.total;
      return res;
    },
    search() {
      this.goodPage = {
        query: this.input3,
        pagenum: 1,
        pagesize: 10,
      };
      this.getGoodsList().then((res) => {
        if (res.data.meta.status !== 200) {
          return this.$message.error("获取商品列表失败");
        } else {
          this.$message.success("获取商品列表成功");
        }
      });
    },
    addGoods() {
      this.$router.push("/goods/add");
    },
    deleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteGoods(id).then((res) => {
            if (res.data.meta.status !== 200) {
              return this.$message.error("删除失败");
            } else {
              this.getGoodsList();
              this.$message.success("删除成功");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleSizeChange(val) {
      // console.log(val);
      this.goodPage.pagesize = val;
      this.getGoodsList();
    },
    handleCurrentChange(val) {
      this.goodPage.pagenum = val;
      this.getGoodsList();
    },
  },
};
</script>
<style lang='less'  scoped>
main {
  width: 100%;
  background-color: #fff;
  margin-top: 15px;
  padding: 20px;
  border-radius: 5px;
  box-shadow: 2px 2px 10px #ccc;
  .title {
    display: flex;
    margin-bottom: 20px;
    .el-input {
      width: 500px;
      margin-right: 20px;
    }
  }
}
</style>
